import { _decorator, Component, director, dynamicAtlasManager, macro, Prefab } from 'cc';
import { DEBUG } from 'cc/env';
import { games } from 'yycore';

import YYExt from '../../yyExt/YYExt';

const { ccclass, property } = _decorator;

macro.CLEANUP_IMAGE_CACHE = false;
dynamicAtlasManager.enabled = true;

@ccclass('GlobalPrefabs')
export class GlobalPrefabs extends Component {

    @property([Prefab])
    private readonly pfGlobals: Prefab[] = [];

    onLoad() {
        // 注册为常驻节点
        director.addPersistRootNode(this.node);

        for (let pb of this.pfGlobals) {
            if (!pb) continue;
            switch (pb.name) {
                case 'YYMessageBox': games.ui.setMessageBoxPrefab(pb); break;
                case 'YYLoading': YYExt.regPublicUI(pb, games.ui.UIType.Loading); break;
                case 'YYGameLoading': YYExt.regPublicUI(pb, games.ui.UIType.GameLoading); break;
                case 'YYTips': YYExt.regPublicUI(pb, games.ui.UIType.Chat); break;
                case 'UISetting': YYExt.regPublicUI(pb, games.ui.UIType.GameSetting); break;
                case 'HelpView': YYExt.regPublicUI(pb, games.ui.UIType.User2); break;
                case 'UILanguage': YYExt.regPublicUI(pb, games.ui.UIType.User3); break;
                case 'GlobalInfoBar': YYExt.regPublicUI(pb, games.ui.UIType.User11); break;
            }
        }
    }

    start() {
        if (director.isPersistRootNode(this.node)) {
            DEBUG && console.debug('# GlobalPrefabs has been persist root ...');
        } else {
            DEBUG && console.warn('# GlobalPrefabs has not been persist root !!!');
        }
    }

}

